package draw_0728_all;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class drawmouse implements MouseListener, ActionListener {
    public int x1,x2,x3,y1,y2,y3;
    public Graphics gr;
    public String name;
    public int flag=1;

    public void actionPerformed(ActionEvent e){
        String st=e.getActionCommand();
        if("蓝色".equals(st)){
            gr.setColor(Color.BLUE);
        }else if("绿色".equals(st)){
            gr.setColor(Color.GREEN);
        }else{
            name=e.getActionCommand();
        }

    }
    public void mouseClicked(MouseEvent e){
        x3=e.getX();
        y3=e.getY();
        if("三角形".equals(name)){
            gr.drawLine(x1,y1,x3,y3);
            gr.drawLine(x2,y2,x3,y3);
            flag=1;
            System.out.print("点击");
        }
        if("多边形".equals(name)){
            gr.drawLine(x3,y3,x2,y2);
            x2=x3;
            y2=y3;
            if( e.getClickCount() == 2){
                gr.drawLine(x1,y1,x3,y3);
                flag=1;
            }

        }

    }
    public void mousePressed(MouseEvent e){
        if(flag==1){
            System.out.print("按下");
            x1=e.getX();
            y1=e.getY();
        }

    }
    public void mouseReleased(MouseEvent e){
       if(flag==1) {
           x2 = e.getX();
           y2 = e.getY();
           System.out.print("松开");
       }
       if("直线".equals(name)){
           gr.drawLine(x1,y1,x2,y2);
       }
       if("矩形".equals(name)){
           int w = Math.abs(x1-x2);
           int h = Math.abs(y2-y1);

           gr.drawRect(x1,y1,w,h);
       }
       if("三角形".equals(name)||"多边形".equals(name)&&flag==1){
           gr.drawLine(x1,y1,x2,y2);
           flag++;
       }


    }
    public void mouseEntered(MouseEvent e){

    }
    public void mouseExited(MouseEvent e){

    }
}
